<template>
  <!-- 上传到开发服务器 -->
  <div class="upload-dev-server">
    <!-- 左侧搜索区域 -->
    <div class="search-content">
      <SearchInput @add="addConfig" @search="searchConfig"></SearchInput>
      <div class="card-list">
        <div class="card-list-content" v-for="card in filterCardList" :key="card._id">
          <ConfigCard
            :configForm="card"
            :selectList="selectList"
            @edit="editConfig"
            @copy="copyConfig"
            @delete="deleteConfig"
            @select="selectConfig"
          >
          </ConfigCard>
        </div>
      </div>
      <div class="empty-content" v-show="filterCardList.length === 0">
        暂无内容
      </div>
    </div>
    <!-- 上方操作区域 -->
    <OperationContent :selectList="selectList"></OperationContent>
    <!-- 下方日志区域 -->
    <LogContent></LogContent>
    <!-- 弹出对话框 -->
    <ConfigDialog
      ref="dialog"
      v-model="showConfigDialog"
      :title="dialogTitle"
      :uploadForm="uploadForm"
      @save="getCardList"
      >
    </ConfigDialog>
  </div>
</template>

<script>
import SearchInput from './components/SearchInput'
import ConfigCard from './components/ConfigCard'
import OperationContent from './components/OperationContent'
import LogContent from './components/LogContent'
import ConfigDialog from './components/ConfigDialog'
import { mapActions } from 'vuex'
export default {
  components: {
    SearchInput,
    ConfigCard,
    OperationContent,
    LogContent,
    ConfigDialog
  },
  data () {
    return {
      showConfigDialog: false, // 显示弹出对话框
      cardList: [], // 配置列表
      dialogTitle: '', // 对话框标题
      uploadForm: {}, // 提交的表单
      selectList: [], // 选择的列表
      filter: '' // 搜索过滤条件
    }
  },
  computed: {
    filterCardList () {
      const filter = this.filter
      return this.cardList.filter(config => {
        return config.configName.indexOf(filter) > -1 || config.buildChild.indexOf(filter) > -1
      })
    }
  },
  created () {
    this.getCardList()
    this.clearLog()
  },
  methods: {
    ...mapActions(['clearLog']),
    // 获取配置列表
    getCardList () {
      this.$db.buildconfig.find().exec((err, docs) => {
        if (err) {
          return
        }
        this.cardList = docs
        this.selectList.splice(0, this.selectList.length)
      })
    },
    // 搜索配置，匹配自定义名称或者打包子路径
    searchConfig (val) {
      this.filter = val
    },
    // 添加配置
    addConfig () {
      this.dialogTitle = '新增配置'
      this.$refs.dialog.resetForm()
      this.showConfigDialog = true
    },
    // 修改配置
    editConfig (configForm) {
      this.dialogTitle = '编辑配置'
      this.uploadForm = configForm
      this.showConfigDialog = true
    },
    // 复制配置
    copyConfig (configForm) {
      this.dialogTitle = '复制配置'
      delete configForm._id
      configForm.configName = configForm.configName + '(复制)'
      this.uploadForm = configForm
      this.showConfigDialog = true
    },
    // 删除配置
    deleteConfig (id) {
      this.$db.buildconfig.remove({ _id: id }, (err, nums) => {
        if (err) {
          return
        }
        console.log(nums)
        this.getCardList()
      })
    },
    // 选择/取消选择
    selectConfig (id) {
      const index = this.selectList.findIndex(list => list._id === id)
      if (index > -1) {
        this.selectList.splice(index, 1)
      } else {
        const config = this.cardList.find(list => list._id === id)
        this.selectList.push(config)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.upload-dev-server{
  display: grid;
  grid-template-columns: auto 300px;
  grid-template-rows: 50% 50%;
  height: 100%;
}
.search-content{
  grid-row-start: 1;
  grid-row-end: 3;
  min-width: 390px;
  overflow-x: hidden;
  overflow-y: auto;
  border-right: 3px solid #efefef;
  background: #fafafa;
  .card-list{
    display: grid;
    justify-content: space-evenly;
    grid-template-columns: repeat(auto-fill, minmax(340px, 450px));
    .card-list-content{
      padding: 10px;
    }
  }
  .empty-content{
    display: flex;
    align-items: center;
    justify-content: center;
    height: calc(100% - 61px);
    width: 100%;
  }
}
</style>